<?php

/**
 * alert actions.
 *
 * @package    explications_informatiques
 * @subpackage alert
 * @author     Guimezanes Cyrille
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class alertActions extends sfActions
{
  

  public function executeNew(sfWebRequest $request)
  {
	$this->forwardUnless($this->getUser()->hasCredential("AJOUT_ALERT"),"headers","forbidden");
    $this->form = new AlertsForm();
	$this->form->setDefault('id_coms', $request->getParameter('id'));
	$this->form->setDefault('id_asso', $request->getParameter('id2'));
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forwardUnless($request->isMethod(sfRequest::POST),"headers","forbidden");
	$this->forwardUnless($this->getUser()->hasCredential("AJOUT_ALERT"),"headers","forbidden");
	
    $this->form = new AlertsForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  

  public function executeDelete(sfWebRequest $request)
  {
	$this->forwardUnless($this->getUser()->hasCredential("SUPPR_ALERT"),"headers","forbidden");

    $this->forward404Unless($alerts = Doctrine_Core::getTable('Alerts')->find(array($request->getParameter('idalerts'))), sprintf('Object alerts does not exist (%s).', $request->getParameter('idalerts')));
    $alerts->delete();
	$this->getUser()->setFlash("info","Signalement supprimé");
	//log
	sfCoreAutoload::logElem("Suppression d'une alerte - id:".$request->getParameter('idalerts'), $this->getUser()->getGuardUser()->getId(), date("Y-m-d h:i:s"));
    $this->redirect('alert/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    $this->forwardUnless($this->getUser()->isAuthenticated(),"headers","forbidden");
	$this->forwardUnless($this->getUser()->hasCredential("AJOUT_ALERT"),"headers","forbidden");
	if ($form->isValid())
    {
      $alerts = new Alerts();
	  $alerts->setIdComs($request["alert"]["id_coms"]);
	  $alerts->setIdReporter($this->getUser()->getGuardUser()->getId());
	  $alerts->setIdAsso($request["alert"]["id_asso"]);
	  $alerts->setRaison($request["alert"]["raison"]);
	  $alerts->setCommentaire($request["alert"]["commentaire"]);
	  $alerts->setDate(date("Ymdhis"));
	  $alerts->save();
	  //Log
	  sfCoreAutoload::logElem("Ajout d'une alerte - id_asso:".$request["alert"]["id_asso"]." coms:".$request["alert"]["id_coms"], $this->getUser()->getGuardUser()->getId(), date("Y-m-d h:i:s"));
	  $this->getUser()->setFlash("info","Votre signalement à été envoyé à l'équipe du site");
      $this->redirect('@show_article?idasso='.$request["alert"]["id_asso"]."&title=article");
    }
  }
}
